﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Products
{
    /// <summary>
    /// This object represents the methods of a Adv.
    /// </summary>
    internal class AttributeValueDAO
    {

        #region general

        private static AttributeValue LoadObj(IDataReader datareader)
        {
            AttributeValue obj = new AttributeValue();
            obj.PAID = MpDataHelp.GetInt(datareader, "PAID");
            obj.AttributeValueID = MpDataHelp.GetInt(datareader, "AttributeValueID");
            obj.AttributeID = MpDataHelp.GetInt(datareader, "AttributeID");
            obj.Value = MpDataHelp.GetString(datareader, "Value");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            return obj;
        }

        #endregion

        public static int AttributeValue_Add(AttributeValue obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_AttributeValue_Add");
            db.AddOutParameter(dbCommand, "AttributeValueID", DbType.Int32, obj.AttributeValueID);
            db.AddInParameter(dbCommand, "AttributeID", DbType.Int32, obj.AttributeID);
            db.AddInParameter(dbCommand, "Value", DbType.String, obj.Value);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@AttributeValueID"));
            return -1;
        }

        public static int ProductAttribute_Add(int PAID, int AttributeValueID, int ProductID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_ProductAttribute_Add");
            db.AddOutParameter(dbCommand, "PAID", DbType.Int32, PAID);
            db.AddInParameter(dbCommand, "AttributeValueID", DbType.Int32, AttributeValueID);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@PAID"));
            return -1;
        }

        public static int AttributeValue_Update(AttributeValue obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_AttributeValue_Update");
            db.AddInParameter(dbCommand, "AttributeValueID", DbType.Int32, obj.AttributeValueID);
            db.AddInParameter(dbCommand, "AttributeID", DbType.Int32, obj.AttributeID);
            db.AddInParameter(dbCommand, "Value", DbType.String, obj.Value);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int ProductAttribute_Update(int PAID, int AttributeValueID, int ProductID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_ProductAttribute_Update");
            db.AddInParameter(dbCommand, "PAID", DbType.Int32, PAID);
            db.AddInParameter(dbCommand, "AttributeValueID", DbType.Int32, AttributeValueID);
            db.AddInParameter(dbCommand, "ProductID", DbType.String, ProductID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int AttributeValue_Delete(int AttributeValueID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_AttributeValue_Delete");
            db.AddInParameter(dbCommand, "AttributeValueID", DbType.Int32, AttributeValueID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int ProductAttribute_Delete(int PAID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_ProductAttribute_Delete");
            db.AddInParameter(dbCommand, "PAID", DbType.Int32, PAID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<AttributeValue> AttributeValue_Select(int AttributeValueID, int AttributeID, int SortOrder)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_AttributeValue_Select");
            db.AddInParameter(dbCommand, "AttributeValueID", DbType.Int32, AttributeValueID);
            db.AddInParameter(dbCommand, "AttributeID", DbType.Int32, AttributeID);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            List<AttributeValue> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<AttributeValue>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }

         public static List<AttributeValue> ProductAttribute_Select(int ProductID,int AttributeValueID, int SortOrder)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_ProductAttribute_Select");
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            db.AddInParameter(dbCommand, "AttributeValueID", DbType.Int32, AttributeValueID);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            List<AttributeValue> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<AttributeValue>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }

    }
}
